package com.jyh.blog.controller;

import com.jyh.blog.annotation.OptLog;
import com.jyh.blog.dto.FriendLinkBackDTO;
import com.jyh.blog.dto.FriendLinkDTO;
import com.jyh.blog.service.FriendLinkService;
import com.jyh.blog.utils.Result;
import com.jyh.blog.vo.ConditionVO;
import com.jyh.blog.vo.FriendLinkVO;
import com.jyh.blog.vo.LinkCheckVO;
import com.jyh.blog.vo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.jyh.blog.constant.OptTypeConst.*;

@Api(tags = "友链管理")
@RestController
public class FriendLinkController {
    @Autowired
    private FriendLinkService friendLinkService;

    /**
     * 首页友链列表
     * @return
     */
    @ApiOperation(value = "首页友链列表")
    @GetMapping("links")
    public Result<List<FriendLinkDTO>> links(){
        return Result.ok(friendLinkService.homeLinks());
    }

    /**
     * 添加或修改友链
     * @return
     */
    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "添加或修改友链")
    @PostMapping("/admin/link")
    public Result<String> saveOrUpdateLinks(@Valid @RequestBody FriendLinkVO friendLinkVO){
        friendLinkService.saveOrUpdateLinks(friendLinkVO);
        return Result.ok();
    }

    /**
     * 审核友链
     * @param linkCheckVO
     * @return
     */
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "审核友链")
    @PutMapping("admin/link")
    public Result<String> linkCheck(@Valid @RequestBody LinkCheckVO linkCheckVO){
        friendLinkService.linkCheck(linkCheckVO);
        return Result.ok();
    }

    /**
     * 后台友链列表
     * @param conditionVO
     * @return
     */
    @ApiOperation(value = "后台友链列表")
    @PostMapping("/admin/links")
    public Result<PageResult<FriendLinkBackDTO>> linkBackDTOList(@RequestBody ConditionVO conditionVO){
        return Result.ok(friendLinkService.linkBackDTOList(conditionVO));
    }

    /**
     * 彻底删除友链
     * @param idList
     * @return
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "物理删除友链")
    @DeleteMapping("admin/links")
    public Result<String> deleteLinks(@RequestBody List<String> idList){
        friendLinkService.deleteLinks(idList);
        return Result.ok();
    }

}
